<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <!-- 始终显示的搜索条件 -->
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="frameNumber">
        <el-input
          v-model="queryParams.frameNumber"
          placeholder="请输入车架号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- 展开后显示的搜索条件 -->
      <template v-if="advanced">
        <el-form-item label="归属" prop="belong">
          <el-input
            v-model="queryParams.belong"
            placeholder="请输入归属"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产权" prop="property">
          <el-input
            v-model="queryParams.property"
            placeholder="请输入产权"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="购买日期" prop="buyTime">
          <el-date-picker
            clearable
            v-model="queryParams.buyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="购车金额" prop="price">
          <el-input
            v-model="queryParams.price"
            placeholder="请输入购车金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="购置税" prop="tax">
          <el-input
            v-model="queryParams.tax"
            placeholder="请输入购置税"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="黄蓝牌" prop="card">
          <el-input
            v-model="queryParams.card"
            placeholder="请输入黄蓝牌"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="油卡编号" prop="fuelCard">
          <el-input
            v-model="queryParams.fuelCard"
            placeholder="请输入油卡编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="油卡金额" prop="fuelPrice">
          <el-input
            v-model="queryParams.fuelPrice"
            placeholder="请输入油卡金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="油耗" prop="fuelConsumption">
          <el-input
            v-model="queryParams.fuelConsumption"
            placeholder="请输入油耗"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="载重" prop="load">
          <el-input
            v-model="queryParams.load"
            placeholder="请输入载重"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车长" prop="length">
          <el-input
            v-model="queryParams.length"
            placeholder="请输入车长"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车宽" prop="wide">
          <el-input
            v-model="queryParams.wide"
            placeholder="请输入车宽"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车高" prop="height">
          <el-input
            v-model="queryParams.height"
            placeholder="请输入车高"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input
            v-model="queryParams.label"
            placeholder="请输入标签"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </template>
      
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="text"
          size="mini"
          @click="advanced = !advanced"
        >
          {{ advanced ? '收起' : '展开' }}
          <i :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['driver:trailer:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['driver:trailer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['driver:trailer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['driver:trailer:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="trailerList"
      @selection-change="handleSelectionChange"
      border
      height="600"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="自生成主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column label="车牌号" align="center" prop="plateNumber" />
      <el-table-column label="车架号" align="center" prop="frameNumber" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="规格型号" align="center" prop="model" />
      <el-table-column label="车型" align="center" prop="type" />
      <el-table-column label="" align="center" prop="remark" />
      <el-table-column label="上线状态" align="center" prop="status" />
      <el-table-column label="归属" align="center" prop="belong" />
      <el-table-column label="产权" align="center" prop="property" />
      <el-table-column
        label="购买日期"
        align="center"
        prop="buyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购车金额" align="center" prop="price" />
      <el-table-column label="购置税" align="center" prop="tax" />
      <el-table-column label="黄蓝牌" align="center" prop="card" />
      <el-table-column label="油卡编号" align="center" prop="fuelCard" />
      <el-table-column label="油卡金额" align="center" prop="fuelPrice" />
      <el-table-column label="油耗" align="center" prop="fuelConsumption" />
      <el-table-column label="载重" align="center" prop="load" />
      <el-table-column label="车长" align="center" prop="length" />
      <el-table-column label="车宽" align="center" prop="wide" />
      <el-table-column label="车高" align="center" prop="height" />
      <el-table-column label="标签" align="center" prop="label" />
      <el-table-column label="车辆图片" align="center" prop="img" />
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['driver:trailer:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['driver:trailer:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        :inline="true"
        class="demo-form-inline"
        style=""
      >
        <!-- <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item> -->
        <el-form-item label="车牌号" prop="plateNumber" class="item">
          <el-input v-model="form.plateNumber" placeholder="请输入车牌号" />
        </el-form-item>

        <el-form-item label="车辆品牌" prop="brand" class="item">
          <el-input
            v-model="form.brand"
            size="small"
            placeholder="请输入品牌"
          />
        </el-form-item>

        <el-form-item label="车辆归属" prop="belong" class="item">
          <el-select v-model="form.belong" size="small" style="width: 200px">
            <el-option
              label="高碑店市金睿智商贸有限公司"
              value="高碑店市金睿智商贸有限公司"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="车型" prop="type" class="item">
          <el-select v-model="form.type" size="small" style="width: 200px">
            <el-option
              :label="i.plateType"
              :value="i.id"
              v-for="i in options"
              :key="i.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="产权归属" prop="property" class="item">
          <el-select v-model="form.property" size="small" style="width: 200px">
            <el-option
              label="高碑店市金睿智商贸有限公司"
              value="高碑店市金睿智商贸有限公司"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车架号" prop="frameNumber" class="item">
          <el-input v-model="form.frameNumber" placeholder="请输入车架号" />
        </el-form-item>
        <el-form-item label="黄蓝牌" prop="card" class="item">
          <!-- <el-input v-model="form.card" placeholder="请输入黄蓝牌" /> -->

          <el-select v-model="form.card" size="small" style="width: 200px">
            <el-option
              :label="i.label"
              :value="i.value"
              v-for="(i, index) in cardOptions"
              :key="index"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="购买日期" prop="buyTime" class="item">
          <el-date-picker
            style="width: 200px"
            clearable
            v-model="form.buyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购买日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="业务类型" prop="businessType" class="item">
          <el-checkbox-group v-model="form.businessType" style="width: 200px">
            <el-checkbox label="救援" name="type"></el-checkbox>
            <el-checkbox label="路修" name="type"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="规格型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="购车金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入购车金额" />
        </el-form-item>
        <el-form-item label="购置税金额" prop="tax">
          <el-input v-model="form.tax" placeholder="请输入购置税" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="油卡编号" prop="fuelCard">
          <el-input v-model="form.fuelCard" placeholder="请输入油卡编号" />
        </el-form-item>
        <el-form-item label="油卡金额" prop="fuelPrice">
          <el-input v-model="form.fuelPrice" placeholder="请输入油卡金额" />
        </el-form-item>
        <el-form-item label="油耗" prop="fuelConsumption">
          <el-input v-model="form.fuelConsumption" placeholder="请输入油耗" />
        </el-form-item>
        <el-form-item label="载重" prop="load">
          <el-input v-model="form.load" placeholder="请输入载重" />
        </el-form-item>
        <el-form-item label="车长" prop="length">
          <el-input v-model="form.length" placeholder="请输入车长" />
        </el-form-item>
        <el-form-item label="车宽" prop="wide">
          <el-input v-model="form.wide" placeholder="请输入车宽" />
        </el-form-item>
        <el-form-item label="车高" prop="height">
          <el-input v-model="form.height" placeholder="请输入车高" />
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入标签" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTrailer,
  getTrailer,
  delTrailer,
  addTrailer,
  updateTrailer,
  listTrailers,
} from "../../../api/driver/trailer/trailer";
import {
  listPlateType,

} from "@/api/driver/trailer/type";
export default {
  name: "Trailer",
  data() {
    return {
      advanced: false, // 控制高级搜索条件的显示与隐藏
      cardOptions: [
        { label: "黄牌", value: "黄牌" },
        { label: "蓝牌", value: "蓝牌" },
      ],
      // 遮罩层
      loading: true,
      options: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户车辆表格数据
      trailerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plateNumber: null,
        frameNumber: null,
        brand: null,
        model: null,
        type: null,
        status: null,
        belong: null,
        property: null,
        buyTime: null,
        price: null,
        tax: null,
        card: null,
        fuelCard: null,
        fuelPrice: null,
        fuelConsumption: null,
        load: null,
        length: null,
        wide: null,
        height: null,
        label: null,
        img: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        plateNumber: [
          { required: true, message: "请输入数据", trigger: "blur" },
        ],
        brand: [{ required: true, message: "请输入数据", trigger: "blur" }],
        belong: [{ required: true, message: "请输入数据", trigger: "blur" }],
        type: [{ required: true, message: "请输入数据", trigger: "blur" }],
        property: [{ required: true, message: "请输入数据", trigger: "blur" }],
        frameNumber: [
          { required: true, message: "请输入数据", trigger: "blur" },
        ],
        buyTime: [{ required: true, message: "请输入数据", trigger: "blur" }],
        card: [{ required: true, message: "请输入数据", trigger: "blur" }],
        businessType: [
          { required: true, message: "请输入数据", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCarList();
  },
  methods: {
    getCarList() {
      listPlateType(this.queryParams).then((response) => {
        this.options = response.rows;
        console.log(this.options, "this.options ");
      });
    },
    /** 查询用户车辆列表 */
    getList() {
      this.loading = true;
      listTrailer(this.queryParams).then((response) => {
        this.trailerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plateNumber: null,
        frameNumber: null,
        brand: null,
        model: null,
        type: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: null,
        belong: null,
        property: null,
        buyTime: null,
        price: null,
        tax: null,
        card: null,
        fuelCard: null,
        fuelPrice: null,
        fuelConsumption: null,
        load: null,
        length: null,
        wide: null,
        height: null,
        label: null,
        img: null,
        businessType: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增车辆基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTrailer(id).then((response) => {
        response.data.businessType = response.data.businessType.split(",");

        this.form = response.data;
        this.open = true;
        this.title = "修改车辆基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      let param = JSON.parse(JSON.stringify(this.form));
      if (param.businessType.length > 0) {
        param.businessType = param.businessType.join(",");
      }
      console.log();

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (param.id != null) {
            updateTrailer(param).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrailer(param).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户车辆编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTrailer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "driver/trailer/export",
        {
          ...this.queryParams,
        },
        `trailer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.el-select .el-input {
  width: 200px !important;
}
::v-deep .item .el-form-item__label {
  color: #4574fe;
}

/* 添加展开收起按钮样式 */
.el-button--text {
  margin-left: 8px;
  font-size: 13px;
}
.el-icon-arrow-up, .el-icon-arrow-down {
  margin-left: 4px;
  transition: all 0.3s;
}
</style>
